import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "Login",
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/Video",
        name: "Video",
        component: () =>
          import(/* webpackChunkName: "video" */ "../views/Video.vue"),
      },
      {
        path: "/Video2",
        name: "Video2",
        component: () =>
          import(/* webpackChunkName: "video" */ "../views/Video2.vue"),
      },
      {
        path: "/CondtionMonit",
        name: "CondtionMonit",
        component: () =>
          import(
            /* webpackChunkName: "CondtionMonit" */ "../views/CondtionMonit.vue"
          ),
      },
      {
        path: "/OnlineMap",
        name: "OnlineMap",
        component: () =>
          import(/* webpackChunkName: "OnlineMap" */ "../views/OnlineMap.vue"),
      },
      {
        path: "/QueryImg",
        name: "QueryImg",
        component: () =>
          import(/* webpackChunkName: "QueryImg" */ "../views/QueryImg.vue"),
      },
      {
        path: "/DeviceManagement",
        name: "DeviceManagement",
        component: () =>
          import(
            /* webpackChunkName: "DeviceManagement" */ "../views/DeviceManagement.vue"
          ),
      },

      {
        path: "/Organizations",
        name: "Organizations",
        component: () =>
          import(
            /* webpackChunkName: "DeviceManagement" */ "../views/Organizations.vue"
          ),
      },
      {
        path: "/UserManage",
        name: "UserManage",
        component: () =>
          import(
            /* webpackChunkName: "UserManage" */ "../views/UserManage.vue"
          ),
      },
      {
        path: "/Log",
        name: "Log",
        component: () =>
          import(/* webpackChunkName: "DeviceManagement" */ "../views/Log.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/Login.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name !== "Login") {
    !localStorage.Authorization ? next({ path: "/login" }) : next();
  } else {
    next();
  }
});
const originalPush = VueRouter.prototype.push
// 重写了原型上的push方法，统一的处理了错误信息
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router;
